var express = require('express');
var router = express.Router();

// 上传图片
var multer = require("multer");
var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './public/images/goods')
  },
  filename: function (req, file, cb) {
    if(!req.arr){
      req.arr = [];
    }
    var suffix = file.mimetype.split("/")[1];
    var fieldname = file.fieldname + '-' + Date.now()+"."+suffix;
    cb(null, file.fieldname + '-' + Date.now()+"."+suffix);
    req.arr.push("/images/goods/"+fieldname);
  }
});
var upload = multer({ storage: storage })


var isLogin = require("../bin/isLogin.js");
var isSeller = require("../bin/isSeller.js");
var launchDAO = require("../bin/db/launchDAO.js");
var classifyDAO = require("../bin/db/classifyDAO.js");

// 添加分类接口
router.post("/home/add",(req,res)=>{
  classifyDAO.findOne({classifyOneName:req.body.classifyOneName})
  .then(data=>{
    if(data){
      for(var i=0;i<data.classifyTwoName;i++){
        if(data.classifyTwoName[i]==req.body.classifyTwoName){
          res.json({err:1,msg:"该分类已存在"});
          return;
        }
      };
      data.classifyTwoName.push(req.body.classifyTwoName);
      data.save()
      .then(()=>{
        res.json({err:0,msg:"添加常用分类成功"});
      });
    }else{
      
      var c = new classifyDAO({
        classifyOneName : req.body.classifyOneName,
        classifyTwoName:req.body.classifyTwoName
      });
      c.save()
      .then(()=>{
        res.json({err:0,msg:"添加分类成功"});
      });
    }
  });
});

// 选择分类接口
router.get("/home/choose",(req,res)=>{
  classifyDAO.find()
  .then(data=>{
    res.json({err:0,data});
  });
})

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});


router.get("/home",function(req,res){

  var data = [
    {
      img:"01.png",
      name:"京东秒杀"
    },
    {
      img:"02.png",
      name:"找折扣"
    },
    {
      img:"03.png",
      name:"领券中心"
    },
    {
      img:"04.png",
      name:"新人权益"
    },
    {
      img:"5.png",
      name:"新品首发"
    },
    {
      img:"6.png",
      name:"京东拼购"
    },
    {
      img:"7.png",
      name:"数码馆"
    },
    {
      img:"8.png",
      name:"服装馆"
    },
    {
      img:"9.png",
      name:"手机馆"
    },
    {
      img:"10.png",
      name:"京东超市"
    },
  ];
  var list = [
    {
      url:"",
      img:"/images/list/list1.JPG"
    },
    {
      url:"",
      img:"/images/list/list2.JPG"
    },
    {
      url:"",
      img:"/images/list/list3.JPG"
    },
    {
      url:"",
      img:"/images/list/list4.JPG"
    },
    {
      url:"",
      img:"/images/list/list5.JPG"
    },
    {
      url:"",
      img:"/images/list/list6.JPG"
    },
    {
      url:"",
      img:"/images/list/list7.JPG"
    },
    {
      url:"",
      img:"/images/list/list8.JPG"
    },
    
  ]

  res.json({err:0,msg:"OK",data,list});
});

// 发布新品
router.post("/home/launch",isLogin);
router.post("/home/launch",isSeller);
router.post("/home/launch",upload.array("pic"),(req,res)=>{

  var l = new launchDAO({
    nameOfGoods:req.body.nameOfGoods,
    price:req.body.price,
    
    classifyOneName:req.body.value.split(",")[0],
    classifyTwoName:req.body.value.split(",")[1],
    launcher:req.session.user._id,
    goodsOfImg:req.arr,
  });
  l.save()
  .then(data=>{
    req.arr = null;
    res.json({err:0,msg:"上传成功"});
  })
  
});

// 首页商品信息
router.get("/home/recommend",(req,res)=>{

  launchDAO.find()
  .skip(0)
  .limit(20)
  .then(data=>{
    res.json({err:0,data});
  })
  
});

// 分类查询接口
router.get("/home/searchList",(req,res)=>{
 launchDAO.find({classifyOneName:req.query.onename,classifyTwoName:req.query.twoname})
 .then(data=>{
   res.json({err:0,data})
 })
});

// 商品详情页面接口
router.get("/home/product",(req,res)=>{
 launchDAO.findOne({_id:req.query.id})
 .then(data=>{
   res.json({err:0,data})
 })
});

// 商品管理接口
router.get("/home/manager",isLogin);
router.get("/home/manager",isSeller);
router.get("/home/manager",(req,res)=>{
  launchDAO.find({launcher:req.session.user._id})
  .then(data=>{
    res.json({err:0,data})
  })
});

// 输入框搜索接口
router.get("/home/search",(req,res)=>{
  launchDAO.find({nameOfGoods:{$regex:req.query.nameOfGoods}})
  .then(data=>{
    res.json({err:0,data})
  })
})

module.exports = router;
